package logger;

use 5.010001;
use strict;
use warnings;

use POSIX qw(strftime);

require Exporter;

our @ISA = qw(Exporter);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration	use logger ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw(
	
) ] );

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw(
	$NOLOG $ERR $MSG $DBG $DBG2
	$LOG_LVL
	initialize_logging
	err
	msg
	dbg
	
);

our $VERSION = '0.01';


# Preloaded methods go here.

### Private constants
my $log_dir = "./logs/";

### Public Constants
my	( $NOLOG, $ERR, $MSG, $DBG, $DBG2 ) =
	( 0,      1,    2,    3,    4 );
my $LOG_LVL = 4;

### Public functions
sub initialize_logging
{
	( $NOLOG, $ERR, $MSG, $DBG, $DBG2 ) =
	( 0,      1,    2,    3,    4 );
	my $LOG_LVL = 4;

	($log_dir) = @_;
	if( $LOG_LVL > $NOLOG )
	{
		mkpath($log_dir) unless -e $log_dir;
	}

	print( form_log_message( "MSG","Logdir:".$log_dir ) );
}

sub err
{
	my ($text) = @_;
	addlog( "ERR", $text );
}

sub msg
{
	if( $LOG_LVL >= $MSG )
	{
		my ($text) = @_;
		addlog( "MSG", $text );
	}
}

sub dbg
{
	if( $LOG_LVL >= $DBG )
	{
		my ($text) = @_;
		addlog( "DBG", $text );
	}
}

### Public functions
sub form_log_message
{
	my ($lvl, $text) = @_;
	return
		strftime('%Y-%m-%d %H:%M:%S', localtime()).
		", pid=".$$.
		", [".$lvl."]: ".
		$text."\n";
}

sub addlog
{
	if( $LOG_LVL <= $NOLOG )
	{
		return;
	}

	my ($lvl, $text) = @_;
	my $log_msg = form_log_message($lvl,$text);

	my $log_file = $log_dir."log_".strftime('%Y%m%d%H', localtime()).".log";

	open FH, ">> $log_file" or die "cannot open $log_file";
	print FH $log_msg;
	close FH;

	if( $LOG_LVL >= $DBG2 )
	{
		print $log_msg;
	}
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

logger - Perl extension for addid logging funtionality.

=head1 SYNOPSIS

  use logger;
  initialize_logging("<logdir>");
  $LOG_LVL = $MSG; #or $ERR, $DBG, $DBG2. Default = DBG2
  err("Add error log message");
  msg("Add a log message");
  dbg("Add a debug log message");

=head1 DESCRIPTION

Adds functions to log different messages acording to a configured log level = $LOG_LVL.
The $LOG_LVL priority is as it follows: $ERR > $MSG > $DBG > $DBG2.
If the log level is set to, lets say, $MSG, then all highet level messages will be logged.
This means that calling dbg("Text") will not result in anything.

$DBG2 means that the error message is printed to the stdout as well.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

D.D.P., E<lt>barden@E<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by D.D.P.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.1 or,
at your option, any later version of Perl 5 you may have available.


=cut
